package es.ats.codington.festival.junit.services;

import static org.junit.Assert.*;
import java.io.FileReader;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import es.ats.codington.festival.domain.Visitor;
import es.ats.codington.festival.junit.utils.ScriptRunner;
import es.ats.codington.festival.services.VisitorService;

public class TVisitorService {
	
	private static VisitorService vs;
	private Visitor visitor;
	
	private static final int peopleId = 1;
	private static final String firstName = "Pantuflo";
	private static final String lastName = "Rodriguez";
	private static final String email = "pantuflo@accenture.es";
	private static final String phone = "666556677";
	private static final String nif = "12.345.678-X";
	private static final String address = "Calle Falsa, 1";
	private static final String username = "pantuflillo";
	private static final String password = "1234";
	private static final Boolean admin = true;
	
	@BeforeClass
    public static void onceExecutedBeforeAll() {
		
		vs=new VisitorService();
		ScriptRunner sr=new ScriptRunner(vs.getVdi().getCon().connect(),false,true);
		try {
			FileReader fr=new FileReader("sql/newcodingtonCDTdb.sql");
			sr.runScript(fr);
		} catch (Exception e) {
			e.printStackTrace();
		}
		vs.getVdi().getCon().disconnect();
        System.out.println("@BeforeClass: onceExecutedBeforeAll");
    }
 
	@Before
    public void executedBeforeEach() {
		
		//this.vs = new VisitorService();
		
		// peopleid, firsname, lastname, email, phone, nif, address, username, password,admin
		this.visitor = new Visitor(peopleId, firstName, lastName, email, phone, 
				nif ,address, username, password, admin);
		/*this.visitor=new Visitor(4, "Borja","Palazon Crespo","fran.p.crespo@accenture.es","673876543",
				"51.990.960-N","C/ Vallecas mola mucho 46","fran.p.crespo","abcd1234",true);*/
		/*this.visitor=new Visitor(1, "Carlos", "Fernandez-Luna Arribas", 
				"carlos.f.arribas@accenture.com", "654789675", "47.467.345-Z", 
				"C/ de los tios guays 69","carlos.f.arribas","abcd1234",true);*/		
        System.out.println("@Before: executedBeforeEach");
    }
	
    
    /**
   	 * Positive testcase for testGetVisitorById of the Visitor class
   	 */
    @Test
    public void testGetVisitorById_Positive() {
    	
    	Visitor visitorEqual= vs.getVisitorById(visitor.getPeopleId());
    	try {
    		System.out.println(visitorEqual.getPeopleId());
    		System.out.println(visitor.getPeopleId());
			assertEquals(visitor.getPeopleId(), visitorEqual.getPeopleId());
    	} catch (Exception ex) {
    		System.out.println("Error: " + ex);
    	}
	}
    
    /**
   	 * Negative testcase for testGetVisitorById of the Visitor class
   	 */
    @Test
    public void testGetVisitorById_Negative() {
    	
    	Visitor visitorEqual= vs.getVisitorById(visitor.getPeopleId());
    	try {
    		System.out.println(visitorEqual.getPeopleId());
    		System.out.println(visitor.getPeopleId());
			assertNotSame(0, visitorEqual.getPeopleId());
    	} catch (Exception ex) {
    		System.out.println("Error: " + ex);
    	}
	}
    
    
    
    /**
   	 * Positive testcase for testCheckLoginVisitor of the Visitor class
   	 */
    @Test
    public void testCheckLoginVisitor_Positive() throws Exception {
		
    	boolean prueba;   	
    	VisitorService visSer = new VisitorService();   	
    	prueba = visSer.checkLoginVisitor(visitor);
    	
    	try {
    		assertEquals(true,prueba);
    	} catch (Exception ex){
    		System.out.println("Error: " + ex);
    	}	
   	}
    
    /**
   	 * Negative testcase for testCheckLoginVisitor of the Visitor class
   	 */
    @Test
    public void testCheckLoginVisitor_Negative() throws Exception {
		
    	boolean prueba;   	
    	VisitorService visSer = new VisitorService();   	
    	prueba = visSer.checkLoginVisitor(visitor);
    	
    	try {
    		assertNotSame(false,prueba);
    	} catch (Exception ex){
    		System.out.println("Error: " + ex);
    	}	
   	}
	
    
   	/**
   	 * Positive testcase for testCreateVisitor of the Visitor class
   	 */
    @Test
   	public void testCreateVisitor_Positive() throws Exception {
		
    	boolean prueba;  
    	
    	VisitorService visSer = new VisitorService(); 	
    	prueba = visSer.createVisitor(visitor);
    	
    	try {
    		assertEquals(true,prueba);
    	} catch (Exception ex){
    		System.out.println("Error: " + ex);
    	}	
   	}

    
    /**
   	 * Negative testcase for testCreateVisitor of the Visitor class
   	 */
    @Test
   	public void testCreateVisitor_Negative() throws Exception {
		
    	boolean prueba;  
    	
    	VisitorService visSer = new VisitorService(); 	
    	prueba = visSer.createVisitor(visitor);
    	
    	try {
    		assertNotSame(false,prueba);
    	} catch (Exception ex){
    		System.out.println("Error: " + ex);
    	}	
   	}
    
    
    
   	/**
   	 * Positive testcase for testCreateVisitor of the Visitor class
   	 */
    @Test
   	public void testSearchVisitor_Positive() throws Exception {
		
    	Visitor visAux = vs.searchVisitor(visitor.getUserName());
    	
    	try {
    		assertEquals(visitor.getUserName(),visAux.getUserName());
    	} catch (Exception ex){
    		System.out.println("Error: " + ex);
    	}	
   	}
    
    /**
   	 * Negative testcase for testCreateVisitor of the Visitor class
   	 */
    @Test
   	public void testSearchVisitor_Negative() throws Exception {
		
    	Visitor visAux = vs.searchVisitor(visitor.getUserName());
    	
    	try {
    		assertNotSame(0,visAux.getUserName());
    	} catch (Exception ex){
    		System.out.println("Error: " + ex);
    	}	
   	}
	
    
    /**
   	 * Positive testcase for testLoginVisitor of the Visitor class
   	 */
    @Test
   	public void testLoginVisitor_Positive() throws Exception {
		
    	/*Visitor visUserName = vs.login(visitor);
    	Visitor visPassword = vs.login(visitor);*/
    	
    	try {
    		/*assertEquals(visitor.getUserName(),visUserName.getUserName());
    		assertEquals(visitor.getPassword(),visPassword.getPassword());*/
    	} catch (Exception ex) {
    		System.out.println("Error: " + ex);
    	}	
   	}
    
    /**
   	 * Negative testcase for testLoginVisitor of the Visitor class
   	 */
    @Test
   	public void testLoginVisitor_Negative() throws Exception {
		
    	//Visitor visAux = vs.login(visitor);
    	
    	try {
    		//assertNotSame(0,visAux.getUserName());
    	} catch (Exception ex){
    		System.out.println("Error: " + ex);
    	}	
   	}
}
